let util = require('../../../utils/util');
let config = require('../../../utils/config.js');
let upload = require('../../../utils/upload.js');
let API_URL = config.API_URL;
let wxMarkerData = [], demandId, recommendId;
let app = getApp();
Page({
    data: {
        images: [
        ],
        images_url: [
        ]
    },
    onLoad: function (options) {
        // 拿到传递过来的参数 deamndid
        console.log(options)
        let that = this;
        let action;
        // 这里是判断是否有推荐 数据从我的 推荐 中过来
        if (options.recommendId != undefined) {
            my.setNavigationBar({
                title: '编辑推荐',
            })
            action = 'edit';
            let recommend = app.globalData.recommends;
            console.log(recommend)
            recommendId = recommend.recommendId;
            let images = recommend.images;
            let images_url = recommend.images;
            console.log(images_url)
            let description = recommend.description;
            let price = recommend.price;
            demandId = recommend.demandId;
            that.setData({
                images: images,
                images_url: images_url,
                description: description,
                price: price,
                action: action
            })
        } else {
            demandId = options.demandId;
            action = 'add';
            that.setData({
                action: action
            })
        }
    },
    // 选择图片
    chooseImage: function (e) {
        let that = this;
        let images = that.data.images;
        let count = 9 - images.length;
        console.log(count)
        my.chooseImage({
            count: count, // 最多可以选择的图片张数
            success: function (res) {
                images = images.concat(res.apFilePaths);
                // 拿到上传的图片 存储并更新到data

                that.setData({
                    images: images
                })
                console.log(images);
            },
            fail: function () {
                // fail
            },
            complete: function () {
                // complete
            }
        })
    },
    // 图片预览
    previewImage: function (e) {
        console.log(e);
        let index = e.currentTarget.dataset.index;
        let images = this.data.images;
        my.previewImage({
            current: index, // 当前显示图片的位置
            urls: images
        })
    },
    // 删除图片
    del_img: function (e) {

        let index = e.currentTarget.dataset.index;
        let images = this.data.images;
        // let images_url = this.data.images_url;
        // console.log(images_url)
        console.log(images[index])
        // let patt = new RegExp("myfile://");
        // console.log(patt.test(images[index]))
        // if (!patt.test(images[index])) {
        //     images_url.splice(index, 1);
        // }
        images.splice(index, 1);
        this.setData({
            images: images
            // images_url: images_url
        })
    },
    // 上传图片
    // submit: function (e) {
    //     let that = this;
    //     let emptyArray = new Array();
    //     let patt = new RegExp("myfile://");
    //     let filePaths = that.data.images.reduce((x, y) => {
    //         let z;
    //         if (patt.test(y)) {
    //             z = x.concat(y);
    //         } else {
    //             z = x;
    //         }
    //         return z;
    //     }, emptyArray);

    //     console.log(filePaths);
    //     // 这里由于上面没运行到 所以这里为空
    //     let img_url = that.data.images_url;
    //     console.log(img_url)
    //     let images = that.data.images;
    //     if (that.data.images.length == 0) {
    //         util.showNotice('请至少上传一张图片')
    //         return;
    //     }
    //     if (e.detail.value.price == '') {
    //         util.showNotice('请输入价格')
    //         return;
    //     }
    //     if (e.detail.value.info == '') {
    //         util.showNotice('请输入宝贝描述')
    //         return;
    //     }

    //     upload.upload(API_URL.upload_img, filePaths, 0, 0, 0, filePaths.length, img_url, function (images_url) {
    //         console.log(images_url)
    //         that.setData({
    //             images_url: images_url
    //         })
    //         let images_url_str = images_url.join('|');
    //         console.log(images_url_str)
    //         let params;
    //         // 在这里判断是否是 编辑 还是 添加
    //         if (that.data.action == 'add') {
    //             params = {
    //                 from: 'album',
    //                 demand_id: demandId,
    //                 price: e.detail.value.price,
    //                 images: images_url_str,
    //                 description: e.detail.value.info
    //             }
    //             util.PRL(API_URL.add_recommend, params)
    //                 .then(res => {
    //                     console.log(res)
    //                     app.globalData.refresh = true;
    //                     my.navigateBack({
    //                         delta: 1
    //                     })
    //                 }).catch(res => {
    //                     console.log(res);
    //                 })
    //         } else {
    //             params = {
    //                 recommend_id: recommendId,
    //                 price: e.detail.value.price,
    //                 images: images,
    //                 description: e.detail.value.info
    //             }
    //             util.PRL(API_URL.recommends_edit, params)
    //                 .then(res => {
    //                     console.log(res)

    //                     app.globalData.refresh = true;
    //                     my.navigateBack({
    //                         delta: 1
    //                     })
    //                 }).catch(res => {
    //                     console.log(res);
    //                 })
    //         }
    //     })
    // },
    submit: function (e) {
        console.log(e);
        let that = this;
        let emptyArray = new Array();
        let patt = new RegExp("myfile://|http://tmp/");
        let filePaths = that.data.images.reduce((x, y) => {
            let z;
            if (patt.test(y)) {
                z = x.concat(y);
            } else {
                z = x;
            }
            return z;
        }, emptyArray);
        console.log(filePaths)
        let img_url = that.data.images_url;
        if (that.data.images.length == 0) {
            util.showNotice('请至少上传一张图片')
            return;
        }
        if (e.detail.value.price == '') {
            util.showNotice('请输入价格')
            return;
        }
        if (e.detail.value.info == '') {
            util.showNotice('请输入宝贝描述')
            return;
        }
        upload.upload(API_URL.upload_img, filePaths, 0, 0, 0, filePaths.length, img_url, function (images_url) {
            console.log(images_url)
            that.setData({
                images_url: images_url
            })
            let images_url_str = images_url.join('|');
            let params;
            if (that.data.action == 'add') {
                params = {
                    from: 'album',
                    demand_id: demandId,
                    price: e.detail.value.price,
                    images: images_url_str,
                    description: e.detail.value.info
                }
                util.PRL(API_URL.add_recommend, params)
                    .then(res => {
                        console.log(res)
                        app.globalData.refresh = true;
                        my.navigateBack({
                            delta: 1
                        })
                    }).catch(res => {
                        console.log(res);
                    })
            } else {
                params = {
                    recommend_id: recommendId,
                    price: e.detail.value.price,
                    images: images_url_str,
                    description: e.detail.value.info
                }
                util.PRL(API_URL.recommends_edit, params)
                    .then(res => {
                        app.globalData.refresh = true;
                        my.navigateBack({
                            delta: 1
                        })
                    }).catch(res => {
                        console.log(res);
                    })
            }
        })
    }
})